//
//  SFQCHCTableViewCell.m
//  GiftThat
//
//  Created by dllo on 16/1/12.
//  Copyright © 2016年 ChuXiang. All rights reserved.
//

#import "SFQCHCTableViewCell.h"


@interface SFQCHCTableViewCell ()
@property (nonatomic,strong) UISwitch *Swith;
@property (nonatomic,strong) UILabel *QCHClab;
@end

@implementation SFQCHCTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self GetData];
    }
    return self;
}
-(void)GetData{
    self.Swith = [[UISwitch alloc]init];
    [self.Swith addTarget:self action:@selector(SwithDidPress:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:self.Swith];

    self.QCHClab = [[UILabel alloc]initWithFrame:CGRectZero];
    self.QCHClab.textAlignment = 1;

    NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
    NSString *cachePath = [path stringByAppendingPathComponent:@"Caches"];
    self.QCHClab.text = [NSString stringWithFormat:@"%.2fM",[self folderSizeAtPath:cachePath]];
    self.QCHClab.font = [UIFont systemFontOfSize:15];
    [self addSubview:self.QCHClab];
}


-(void)layoutSubviews{
    [super layoutSubviews];
    self.Swith.frame = CGRectMake(self.width - 70, 15, 70, self.height - 40);
      self.QCHClab.frame = CGRectMake(self.width - 140 , 15, 60 , self.height - 40);
    
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"Color"] isEqualToString:@"BlackColor"]) {
        self.QCHClab.textColor = [UIColor whiteColor];
        self.backgroundColor = [UIColor colorWithRed:37 / 255.0 green:36 / 255.0 blue:40 / 255.0 alpha:1.0];

    }else{
        self.QCHClab.textColor = [UIColor blackColor];
        self.backgroundColor = [UIColor whiteColor];

    }

}

-(void)SwithDidPress:(UISwitch *)sender{
    //获取完整路径
    NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
    path = [path stringByAppendingPathComponent:@"Caches"];
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:path]) {
        NSArray *fileNameAarray = [manager subpathsAtPath:path];
        for (NSString *fileName in fileNameAarray) {
            //拼接绝对路径
            NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
            
            //通过文件管理者删除文件
            [manager removeItemAtPath:absolutePath error:nil];
        }
    }
    _QCHClab.text = [NSString stringWithFormat:@"%.2fM",[self folderSizeAtPath:path]];
}
#warning 计算单个文件大小的方法
-(long long)fileSizeAtPath:(NSString *)path
{
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:path]) {
        return [[manager attributesOfItemAtPath:path error:nil]fileSize];
    }
    return 0;
}
#warning  计算一个文件夹的大小
-(float)folderSizeAtPath:(NSString *)path
{
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:path]) {
        return 0;
    }
    //根据路径获取文件夹里面元素集合
    //获取集合类型的枚举类型
    NSEnumerator *enumeratot = [[manager subpathsAtPath:path] objectEnumerator];
    //每次遍历得到的文件名
    //文件夹的大小
    NSString *fileName = [NSString string];
    float folderSize = 0;
    while ((fileName = [enumeratot nextObject])!= nil) {
        NSString *absolutePath = [path stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:absolutePath];
    }
    return folderSize/(1024*1024.0);
}













- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
